/**
 * Created by iallai on 2/1/2018.
 */

import axios from 'axios'
import {CONFIG} from '@/config/env'

let API=CONFIG.PROJECT.API;

/**
 * 查询货源列表,我的货源列表
 * @param {Object} query - 查询参数
 * @param {string} query.size
 * @param {string} query.page
 * @param {string} query.keyword
 * @param {string} config.child 子资源名
 */

const query = (query = {}, config) => new Promise((resolve, reject) => {

  let childname = config && config.child ? `/${config.child}` : '';

  axios.get(`${API.URL}/${API.APPNAME}/supplies${childname}`, {
    params: query,

  }).then(function(res){
    if (API.isOK(res.data)) {
      resolve(res.data);
    }
  })
    .catch(function(err){
      reject("网络错误");
    });


});


const get = (id, config) => new Promise((resolve, reject) => {
  let childname = config && config.child ? `/${config.child}` : '';

  axios.get(`${API.URL}/${API.APPNAME}/supplies${childname}/${id}`).then(function (res) {
    if (API.isOK(res.data)) {
      resolve(res.data);
    }
  })
    .catch(function (err) {
      reject("网络错误");
    });


});
/**
 * 添加货源
 * @param data
 */
const add = (data) => new Promise((resolve, reject) => {


  axios({
    method: 'post',
    url: `${API.URL}/${API.APPNAME}/supplies`,
    data: data,

  }).then(function(res){
    if (API.isOK(res.data)) {
      resolve(res.data);
    } else {
      reject(res.data.message);
    }
  }).catch(function (err) {
    reject("网络错误");
  });


});
/**
 * 编辑货源
 * @param data
 */
const update = (data) => new Promise((resolve, reject) => {


  axios({
    method: 'put',
    url: `${API.URL}/${API.APPNAME}/supplies/${data.id}`,
    data: data,

  }).then(function(res){
    if (API.isOK(res.data)) {
      resolve(res.data);
    } else {
      reject(res.data.message);
    }
  }).catch(function (err) {
    reject("网络错误");
  });


});


const mutiDel = (ids) => new Promise((resolve, reject) => {


  axios({
    method: 'delete',
    url: `${API.URL}/${API.APPNAME}/supplies/muti`,
    data: {
      supply_ids: ids
    },

  }).then(function (res) {
    if (API.isOK(res.data)) {
      resolve(res.data);
    } else {
      reject(res.data.message);
    }
  }).catch(function (err) {
    reject("网络错误");
  });


});
const mutiPublish = (ids, code) => new Promise((resolve, reject) => {

  axios({
    method: 'post',
    url: `${API.URL}/${API.APPNAME}/supplies/muti/publish`,
    data: {
      supply_ids: ids,
      is_shelf_off: code
    },

  }).then(function (res) {
    if (API.isOK(res.data)) {
      resolve(res.data);
    } else {
      reject(res.data.message);
    }
  }).catch(function (err) {
    reject("网络错误");
  });


});

const marketPrice = (data) => new Promise((resolve, reject) => {


  axios.get(`${API.URL}/vehicles/market_price`, {
    params: data
  }).then(function (res) {
    if (API.isOK(res.data)) {
      resolve(res.data);
    }
  })


});


export {query, get, mutiDel, mutiPublish, update, add, marketPrice};

